package com.delivery.www.servlet;

import com.delivery.www.po.Order;
import com.delivery.www.po.Seller;
import com.delivery.www.po.Store;
import com.delivery.www.service.ServiceImpl.OrderServiceImpl;
import com.delivery.www.service.ServiceImpl.StoreServiceImpl;

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import static com.delivery.www.servlet.CouponPage.couponPage;
import static com.delivery.www.servlet.SellerCommentPage.commentPage;
import static com.delivery.www.servlet.SellerPage.sellerPage;

public class SellerStorePage {
    public static void storePage(Seller seller) {
        int i = seller.getSellerId();
        StoreServiceImpl storeService = new StoreServiceImpl();
        OrderServiceImpl orderService = new OrderServiceImpl();
        Store store = storeService.getOwnStore(i);
        System.out.println("======================================");
        System.out.println("店铺名:" + store.getStoreName());
        System.out.println("店铺简介:" + store.getIntroduce());
        System.out.println("店铺菜品数:" + store.getDishNum());
        System.out.println("店铺评论数:" + store.getCommentNum());
        System.out.println("店铺完成订单数:" + store.getProcessOrderNum());
        System.out.println("店铺未处理订单数:" + store.getUnprocessOrderNum());
        System.out.println("======================================");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要进行的操作");
        System.out.println("1.修改店铺信息");
        System.out.println("2.查看菜品");
        System.out.println("3.查看评论");
        System.out.println("4.查看已完成订单");
        System.out.println("5.查看待处理的订单");
        System.out.println("6.设置优惠券");
        System.out.println("7.返回");
        int i1 = scanner.nextInt();
        while (i1 != 1 && i1 != 2 && i1 != 3 && i1 != 4 && i1 != 5 && i1 != 6 && i1 != 7) {
            System.out.println("输入错误！请重新输入");
            i1 = scanner.nextInt();
        }
        if (i1 == 1) {
            System.out.println("请输入你要修改的店铺信息");
            System.out.println("1.修改店铺名");
            System.out.println("2.修改店铺简介");
            int i2 = scanner.nextInt();
            while (i2 != 1 && i2 != 2) {
                System.out.println("输入错误！请重新输入");
                i2 = scanner.nextInt();
            }
            if (i2 == 1) {
                System.out.println("请输入新的店铺名");
                String next = scanner.next();
                boolean b = storeService.updateStoreName(store.getStoreId(), next);
                if (b) {
                    System.out.println("修改成功!即将跳转页面");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    sellerPage(seller);
                }
            } else {
                System.out.println("请输入新的店铺简介");
                String next = scanner.next();
                boolean b = storeService.updateStoreIntroduce(store.getStoreId(), next);
                if (b) {
                    System.out.println("修改成功!即将跳转页面");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    sellerPage(seller);
                }
            }
        }
        if (i1 == 2) {
            SellerDishPage.dishesList(store.getStoreId(), seller);
        }
        if (i1 == 3) {
            commentPage(store.getStoreId(), seller);
        }
        if (i1 == 4) {
            List<Order> orders = orderService.getCompleteOrder(store.getStoreId(), "完成");
            for (Order order : orders) {
                System.out.println("===============================");
                System.out.println("订单编号：" + order.getOrderId());
                System.out.println("下单用户：" + order.getUserName());
                System.out.println("下单菜品id：" + order.getDishId());
                System.out.println("下单金额：" + order.getMoney());
                System.out.println("===============================");
            }
            Float f = orderService.earnings(store.getStoreId());
            System.out.println("收益：" + f + "元");
            System.out.println("返回请按0");
            int i2 = scanner.nextInt();
            while (i2 != 0) {
                System.out.println("输入错误！请重新输入");
                i2 = scanner.nextInt();
            }
            storePage(seller);
        }
        if (i1 == 5) {
            List<Order> orders = orderService.getUnCompleteOrder(store.getStoreId(), "完成");
            for (Order order : orders) {
                System.out.println("订单编号：" + order.getOrderId());
                System.out.println("下单用户：" + order.getUserName());
                System.out.println("下单菜品id：" + order.getDishId());
                System.out.println("下单金额：" + order.getMoney());
                System.out.println("状态：" + order.getStatus());
            }
            System.out.println("是否要修改状态");
            System.out.println("1.是");
            System.out.println("2.否");
            int i2 = scanner.nextInt();
            while (i2 != 1 && i2 != 2) {
                System.out.println("输入错误！请重新输入");
                i2 = scanner.nextInt();
            }
            if (i2 == 1) {
                System.out.println("请输入你要修改的订单id：");
                int i3 = scanner.nextInt();
                System.out.println("1.修改为已接单");
                System.out.println("2.修改为完成");
                int i4 = scanner.nextInt();
                while (i4 != 1 && i4 != 2) {
                    System.out.println("输入错误！请重新输入");
                    i4 = scanner.nextInt();
                }
                boolean b = false;
                if (i4 == 1) {
                    b = orderService.modifyStatus(i3, "已接单", store.getStoreId());
                } else {
                    b = orderService.modifyStatus(i3, "完成", store.getStoreId());
                }
                if (b) {
                    System.out.println("修改成功！即将跳转");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    storePage(seller);
                }
            } else {
                storePage(seller);
            }
        }
        if (i1 == 6) {
            couponPage(store.getStoreId(), seller);
        }
        if (i1 == 7) {
            sellerPage(seller);
        }
    }
}
